/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 11:23:08 AM
 * To change this template use File | Settings | File Templates.
 */

import java.util.*;
import org.jdom.*;

// Referenced classes of package dk.brics.dsd:
//            Declaration, Regexp, Schema, Context,
//            Validator

class ContentsDeclaration extends Declaration
{

    ContentsDeclaration(Element element, Schema schema)
    {
        regexps = new ArrayList();
        Iterator iterator = element.getChildren().iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            Element element1 = (Element)iterator.next();
            if(Schema.isDSDElement(element1))
            {
                String s = element1.getName();
                if(s.equals("normalize"))
                {
                    whitespace = element1.getAttributeValue("whitespace");
                    ccase = element1.getAttributeValue("case");
                } else
                if(s.equals("default"))
                    ddefault = element1.getContent();
                else
                    regexps.add(Regexp.parse(element1, schema));
            }
        } while(true);
    }

    public Element toXML(Context context)
    {
        Element element = new Element("contents", "http://www.brics.dk/DSD/2.0");
        for(Iterator iterator = regexps.iterator(); iterator.hasNext(); element.addContent(((Regexp)iterator.next()).toXML(context)));
        return element;
    }

    void checkRequirements(Context context, Set set)
    {
        Iterator iterator = regexps.iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            Regexp regexp = (Regexp)iterator.next();
            byte byte0 = regexp.matchContents(context, set);
            if(byte0 == 2)
                context.v.invalid(context, "contents of element " + Validator.getElementName(context.e) + " does not match declaration", regexp.toXML(context));
            else
            if(byte0 == 3)
                context.v.invalid(context, "contents of element " + Validator.getElementName(context.e) + " does not always match declaration", regexp.toXML(context));
            else
            if(byte0 == 4)
                context.v.invalid(context, "contents of element " + Validator.getElementName(context.e) + " may not match declaration", regexp.toXML(context));
        } while(true);
    }

    void findDeclarations(List list)
    {
        list.add(this);
    }

    List getDefaultContents()
    {
        if(ddefault != null)
        {
            ArrayList arraylist = new ArrayList();
            Object obj1;
            for(Iterator iterator = ddefault.iterator(); iterator.hasNext(); arraylist.add(obj1))
            {
                Object obj = iterator.next();
                obj1 = null;
                if(obj instanceof Text)
                {
                    obj1 = ((Text)obj).clone();
                    continue;
                }
                if(obj instanceof Element)
                {
                    obj1 = ((Element)obj).clone();
                    continue;
                }
                if(obj instanceof Comment)
                {
                    obj1 = ((Comment)obj).clone();
                    continue;
                }
                if(obj instanceof ProcessingInstruction)
                {
                    obj1 = ((ProcessingInstruction)obj).clone();
                    continue;
                }
                if(obj instanceof CDATA)
                {
                    obj1 = ((CDATA)obj).clone();
                    continue;
                }
                if(obj instanceof EntityRef)
                    obj1 = ((EntityRef)obj).clone();
            }

            return arraylist;
        } else
        {
            return null;
        }
    }

    String getContentsWhitespace()
    {
        return whitespace;
    }

    String getContentsCase()
    {
        return ccase;
    }

    List regexps;
    String whitespace;
    String ccase;
    List ddefault;
}
